package com.ac.driver.mqtt.subscriber;

import com.ac.driver.mqtt.service.StateService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xj.common.bean.constant.Common;
import com.xj.common.bean.em.ControlStateEm;
import com.xj.common.model.ControlInfo;
import com.xj.common.sdk.bean.driver.DriverContext;
import com.xj.common.sdk.service.DriverService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @ClassName : MqttDeviceRegister
 * @Description : 设备接入
 * @Author : Alan
 * @Date: 2020-09-08 15:37
 */
@Slf4j
@Component
public class MqttDeviceHandler implements MessageHandler {


    @Resource
    private DriverService driverService;
    @Resource
    private StateService stateService;
    @Resource
    private DriverContext driverContext;
    @ServiceActivator(inputChannel = "topic-device")
    @Override
    public void handleMessage(Message<?> message) throws MessagingException {

        log.info("设备信息信息:{}",message);
        String payload = (String) message.getPayload();
        MessageHeaders headers = message.getHeaders();

        String mqttReceivedTopic = headers.get("mqtt_receivedTopic", String.class);

        //先解析出重要信息
        String command = mqttReceivedTopic.substring(mqttReceivedTopic.lastIndexOf("/")+1);
        String [] aaa= mqttReceivedTopic.split("/");
        long deviceId = driverContext.getDriverMetadata().getDeviceSnMap().get(aaa[2]).getId();

        switch (command){
            case "response": {//doResponse(dataBody,header,deviceId);
                 break;}
            case "state": {doState(payload,deviceId); break;}
            case "event": {//doEvent(dataBody,header,deviceId);
                 }
            default:{
                log.info("暂不支持的主题操作:{}",command);
            }
        }

        /*String payload = (String) message.getPayload();
        MessageHeaders headers = message.getHeaders();

        JSONObject payloadJson = JSONObject.parseObject(payload);

        log.info("发送pointvalue到kafka上");
        PointValue pointValue = new PointValue();
        pointValue.setCalculateValue(123);
        pointValue.setCreateTime(new Date());
        pointValue.setDeviceId(12l);
        pointValue.setPointId(121l);
        pointValue.setRawValue("10.0");
        pointValue.setUnit("C");
        pointValue.setValue("123");
        pointValue.setOriginTime(new Date());

        driverService.pointValueSender(pointValue);
        String commandType = payloadJson.getString("type");
        String mqttReceivedTopic = headers.get("mqtt_receivedTopic", String.class);
        String command = mqttReceivedTopic.substring(mqttReceivedTopic.lastIndexOf("/")+1);


        switch (commandType){


            default:{
                log.info("暂不支持的主题操作:{}",commandType);
            }

        }*/
    }

    private void doState(String payload, long deviceId){
        log.info("空调设备上送状态信息处理:{}",payload,deviceId);
        stateService.doChargeState(deviceId,payload);
    }


}
